package com.example.exercise.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import com.example.exercise.demo.vo.AppCollectVO;
import com.example.exercise.demo.service.AppCollectService;
import com.example.exercise.common.base.BaseResponse;

/**
* 
*/
@RestController
@RequestMapping("/appCollect")
@Tag(name = "", description = "")
public class AppCollectController {

    @Autowired
    private AppCollectService appCollectService;

    @Operation(summary = "分页查询列表", description = "分页查询列表")
    @RequestMapping(value = "findByWhere", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<Page<AppCollectVO>> findByWhere(@RequestBody AppCollectVO appCollectVO, Page<AppCollectVO> page){
        BaseResponse<Page<AppCollectVO>> baseResponse = new BaseResponse<>();
        List<AppCollectVO> list = appCollectService.findByWhere(page, appCollectVO);
        page.setRecords(list);
        baseResponse.ok(page);
        return baseResponse;
    }

    @Operation(summary = "根据id查详情", description = "根据id查详情")
    @RequestMapping(value = "findByKey", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<AppCollectVO> findByKey(@RequestBody AppCollectVO appCollectVO){
        BaseResponse<AppCollectVO> baseResponse = new BaseResponse<>();
        AppCollectVO appCollectVO1 = appCollectService.findByKey(appCollectVO);
        if(appCollectVO1 == null){
            baseResponse.errMsg("数据为空");
        }else{
            baseResponse.ok(appCollectVO1);
        }
        return baseResponse;
    }

    @Operation(summary = "添加一条记录", description = "添加一条记录")
    @RequestMapping(value = "insertAppCollect", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Object> insertAppCollect(@RequestBody AppCollectVO appCollectVO){
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int count = appCollectService.insert(appCollectVO);
        if(count > 0){
            baseResponse.ok();
        }else{
            baseResponse.errMsg("新增数据失败");
        }
        return baseResponse;
    }

    @Operation(summary = "修改一条记录", description = "修改一条记录")
    @RequestMapping(value = "updateAppCollectByKey", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Object> updateAppCollectByKey(@RequestBody AppCollectVO appCollectVO){
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int count = appCollectService.updateByKey(appCollectVO);
        if(count > 0){
            baseResponse.ok();
        }else{
            baseResponse.errMsg("修改数据失败");
        }
        return baseResponse;
    }

    @Operation(summary = "删除一条记录", description = "删除一条记录")
    @RequestMapping(value = "deleteByKey", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Object> deleteByKey(@RequestBody AppCollectVO appCollectVO){
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int count = appCollectService.deleteByKey(appCollectVO);
        if(count > 0){
            baseResponse.ok();
        }else{
            baseResponse.errMsg("删除数据失败");
        }
        return baseResponse;
    }

}